<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<script type="text/javascript">
	var purViewTree;
	var on_off=false; 
	function uncheck_cascade(node){
		var child=node.children;
		for(var i=0;i<child.length;i++){
			if (purViewTree.tree('find', child[i].id)) {
				purViewTree.tree('uncheck', purViewTree.tree('find', child[i].id).target);
			}
			if(child[i].children!=null){
				uncheck_cascade(child[i]);
			}
		}
	}
	function check_cascade(node){
		if (purViewTree.tree('find', node.pid)) {
			purViewTree.tree('check', purViewTree.tree('find', node.pid).target);
		}
		var pNode=purViewTree.tree('find', node.pid);
		if(pNode.pid!=0){
			check_cascade(pNode);
		}
	}
	$(function() {	
		purViewTree = $('#purViewTree').tree({
			url : '${ctx}/purview/getPurviewAndOperation',
			parentField : 'pid',
			lines : true,
			checkbox : true,
			onClick : function(node) {
				
			},
			onLoadSuccess : function(node, data) {
				progressLoad();
				$.post( '${ctx}/role/getRolePurview', {
					rId : '${rId}'
				}, function(result) {
					if (result.length > 0) {
						for ( var i = 0; i < result.length; i++) {
							if (purViewTree.tree('find', result[i].purId)) {
								purViewTree.tree('check', purViewTree.tree('find', result[i].purId).target);
							}
						}
						on_off=true;
					}else{
						on_off=true;
					}
					
				}, 'json');
				progressClose();
				
			},
			onLoadError:function(e){
				parent.$.messager.alert('错误',"获取权限点数据失败，服务器错误！", 'error');
			},
			cascadeCheck : false,
			onCheck: function (node) {
				 if(on_off){
					on_off=false; 
					if(!node.checked){
						purViewTree.tree('check', purViewTree.tree('find', node.id).target);
						if(node.pid!=0){
							check_cascade(node);
						}
						if(node.children!=null){
							checkChildren(node);
						}
						on_off=true;
					}else{
						purViewTree.tree('uncheck', purViewTree.tree('find', node.id).target);
						if(node.children!=null){
							uncheck_cascade(node);
						}
						on_off=true;
					}
					
				}
				 } 
		});

		$('#roleGrantForm').form({
			url : '${ctx}/role/grant',
			onSubmit : function() {
				progressLoad();
				var isValid = $(this).form('validate');
				if (!isValid) {
					progressClose();
				}
				var checknodes = purViewTree.tree('getChecked');
				var ids = [];
				if (checknodes && checknodes.length > 0) {
					for ( var i = 0; i < checknodes.length; i++) {
						ids.push(checknodes[i].id);
					}
				}
				$('#purIds').val(ids);
				return isValid;
			},
			success : function(result) {
				progressClose();
				//result = $.parseJSON(result);
				if (result=="授权成功") {
					parent.show("授权成功！","提示");
					parent.$.modalDialog.openner_dataGrid.datagrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_dataGrid这个对象，是因为role.jsp页面预定义好了
					parent.loadData();
					parent.$.modalDialog.handler.dialog('close');
				} else {
					parent.$.messager.alert('错误', result, 'error');
				}
			}
		});
	});
	
	/*全选、全不选、补选*/
	function checkInverse() {
		on_off=false;
		var unchecknodes = purViewTree.tree('getChecked', 'unchecked');
		var checknodes = purViewTree.tree('getChecked','checked');
		if (unchecknodes && unchecknodes.length > 0) {
			for ( var i = 0; i < unchecknodes.length; i++) {
				purViewTree.tree('check', unchecknodes[i].target);
			}
			
		}else{
		     for ( var i = 0; i < checknodes.length; i++) {
		    	 var node = $('#purViewTree').tree('find', checknodes[i].id);  
		    	 $('#purViewTree').tree('uncheck', node.target);
			} 
			
		}
		on_off=true;
	}
	function checkChildren(node){
		var child=node.children;
		for(var i=0;i<child.length;i++){
			if (purViewTree.tree('find', child[i].id)) {
				purViewTree.tree('check', purViewTree.tree('find', child[i].id).target);
			}
			if(child[i].children!=null){
				checkChildren(child[i]);
			}
		}
	}
</script>
<div id="roleGrantLayout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'west'" title="权限点" style="width: 300px; padding: 10px;overflow:auto;">
		<div class="well well-small">
			<form id="roleGrantForm" method="post">
				<input name="rId" type="hidden"  value="${rId}" readonly="readonly">
				<ul id="purViewTree"></ul>
				<input id="purIds" name="purIds" type="hidden" />
			</form>
		</div>
	</div>
	
	<div data-options="region:'center'" title="" style="overflow: hidden; padding: 10px;">
		<div>
			<button class="btn btn-success" onclick="checkInverse();">全选</button>
			<br /> <br />
		</div>
	</div>
</div>